package lium.lbs.lbsim.activity

import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.Toast
import com.buz.hjcdriver.bean.AResultBean
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import lium.lbs.lbsim.utils.callbck.DialogCallback
import lium.lbs.lbsim.utils.callbck.JsonCallback
import lium.lbs.lbsim.utils.ToastUtils
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.lzy.okgo.model.Response
import com.lzy.okgo.utils.OkLogger
import kotlinx.android.synthetic.main.activity_renzheng.*
import lium.lbs.lbsim.App
import lium.lbs.lbsim.R
import lium.lbs.lbsim.utils.IAlertDialog
import lium.lbs.lbsim.utils.SpUtils
import java.io.File
import java.util.HashMap

class RenZhengActivity : BaseActivity() {

    override fun getLayoutId(): Int {
        return R.layout.activity_renzheng
    }

    var image1: String? = null
    var image2: String? = null
    var image3: String? = null
    internal var dialog: IAlertDialog? = null
    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("实名认证")
        addOnClickListeners(R.id.btn_submit, R.id.idcardBefore, R.id.idcardAfter, R.id.idcardHand)
    }


    override fun onClick(v: View?) {
        super.onClick(v)
        when (v!!.id) {
            R.id.btn_submit -> {
                submit()
            }
            R.id.idcardBefore -> {
                requestPerFailed()
                openGallery(1)
            }
            R.id.idcardAfter -> {
                requestPerFailed( )
                openGallery(2)
            }
            R.id.idcardHand -> {
                requestPerFailed( )
                openGallery(3)
            }
        }
    }

    private fun submit6() {
        if (TextUtils.isEmpty(image1) || TextUtils.isEmpty(image2) || TextUtils.isEmpty(image3)) {
            ToastUtils.showToast("认证照片不能为空！")
            return
        }

        val param = HashMap<String, String>()
        postDataWithFile(
            "/member/updMemberInfo",
            "idcardBefore",
            param,
            File(image1),
            object : DialogCallback<ResponseBean<AResultBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                    submit2()
                }
            })
    }

    private fun submit2() {
        val param = HashMap<String, String>()
        postDataWithFile(
            "/member/updMemberInfo",
            "idcardAfter",
            param,
            File(image2),
            object : DialogCallback<ResponseBean<AResultBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                    submit3()
                }
            })
    }

    private fun submit3() {
        val param = HashMap<String, String>()
        postDataWithFile(
            "/member/updMemberInfo",
            "idcardHand",
            param,
            File(image3),
            object : DialogCallback<ResponseBean<AResultBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                    ToastUtils.showToast(response.body().msg + "")
                    finish()
                }
            })
    }


    private fun submit() {
        if (TextUtils.isEmpty(image1) || TextUtils.isEmpty(image2) || TextUtils.isEmpty(image3)) {
            ToastUtils.showToast("认证照片不能为空！")
            return
        }
        var file1 = File(image1)
        var file2 = File(image2)
        var file3 = File(image3)
        OkLogger.e("file1:" + file1.exists() + " file2:" + file1.exists() + "  file3:" + file3.exists())
        val fileParam = HashMap<String, File>()
        fileParam["idcardBefore"] = file1
        fileParam["idcardAfter"] = file2
        fileParam["idcardHand"] = file3
        val param = HashMap<String, String>()

        postDataWithFile(
            "/member/updMemberInfo",
            fileParam,
            param,
            object : JsonCallback<ResponseBean<AResultBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                    ToastUtils.showToast(response.body().msg)
                    setResult(200)
                    finish()
                }
            })
    }

    fun openGallery(requestcode: Int) {
        (application as App).initApp()
        PictureSelector.create(this)
            .openGallery(PictureMimeType.ofImage())
            .maxSelectNum(1)
            .isCamera(true)
            .compress(false) //是否压缩
            .compressSavePath(Constants.IMAGE_CACHE_DIR)
            .selectionMode(PictureConfig.MULTIPLE)
            .forResult(requestcode)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            val selectList = PictureSelector.obtainMultipleResult(data)
            selectList[0]?.let {
                when (requestCode) {
                    1 -> {
                        image1 = selectList[0]!!.path
                        displayImageFile(File(selectList[0]!!.path), idcardBefore)
                    }
                    2 -> {
                        image2 = selectList[0]!!.path
                        displayImageFile(File(selectList[0]!!.path), idcardAfter)
                    }
                    3 -> {
                        image3 = selectList[0]!!.path
                        displayImageFile(File(selectList[0]!!.path), idcardHand)
                    }
                }
            }
        }
    }

    fun requestPerFailed( ) {
        val pm = packageManager
        val permission = PackageManager.PERMISSION_GRANTED == pm.checkPermission(
            "android.permission.ACCESS_NETWORK_STATE",
            "com.lkn.a11509.democollection"
        )
        if (!permission) {
            if (dialog == null) {
                dialog = IAlertDialog(this, IAlertDialog.LayoutStyle.DEFAULT, Gravity.CENTER)
                dialog!!.setMessage("你没有打开相机的权限是否去获取是去设置?")
                dialog!!.setPositiveMsg("确定")
                dialog!!.setPositiveOnClickListener { dialog, which ->
                    dialog.dismiss()

                }
                dialog!!.setNegativeMsg("再想想")
                dialog!!.setNegativeOnClickListener { dialog, which ->
                    dialog.dismiss()
                }
            }
            dialog!!.show()
        }

    }

    override fun getNetWorkData() {
    }
}

